package com.siqi.hr.system.common.utils;

import com.siqi.hr.common.utils.Const;
import com.siqi.hr.common.utils.json.JSONArray;
import com.siqi.hr.common.utils.json.JSONException;
import com.siqi.hr.common.utils.json.JSONObject;
import com.siqi.hr.system.common.model.filter.QueryDataEntity;
import com.siqi.hr.system.common.sql.Condition;
import com.siqi.hr.system.common.sql.SField;
import com.siqi.hr.system.common.sql.STable;
import com.siqi.hr.system.organization.entity.Dept;
import com.siqi.hr.system.organization.entity.Employee;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.LinkedHashSet;
import java.util.Set;

public class QueryDataUtil {
	private static Logger log = Logger.getLogger(QueryDataUtil.class);

	public static QueryDataEntity getQueryDataEntity(HttpServletRequest request) {
		HttpSession session = request.getSession();
		QueryDataEntity entity = new QueryDataEntity();
        //获取实体id(也就是tableCode)
		String tableCode = request.getParameter(Const.TABLE_CODE) == null ? "" : request.getParameter(Const.TABLE_CODE);
		//设置tableCode(也就是id)
		entity.setTableCode(tableCode.equals("") ? null : Integer.valueOf(Integer.parseInt(tableCode)));
		//设置表名
		entity.setTableName(STable.TABLE_HEAD+tableCode);
		//获取实体字段信息
		String fields = request.getParameter(Const.FIELDS);
        //设置表字段信息
		if (fields != null) {
			entity.setFields(fields.split(","));
		}
		//获取是查询弹窗列表数据还是查询列表数据标识
		Boolean isTable = request.getParameter(Const.ISTABLE) == null ? false : Boolean.valueOf(request.getParameter(Const.ISTABLE));
		entity.setTable(isTable);
		entity.setEmployee((Employee) session.getAttribute("emp"));
		entity.setDept((Dept) session.getAttribute("dept"));
		//获取排序字段名
		String sort = request.getParameter(Const.BOOTSTRAP_SORT);
		//获取排序方式(asc:升序，desc:降序)
		String order = request.getParameter(Const.BOOTSTRAP_ORDER);
		//获取每页数据第一条数据的偏移量
		int offset = request.getParameter(Const.BOOTSTRAP_OFFSET) == null ? 0 : Integer.valueOf(request.getParameter(Const.BOOTSTRAP_OFFSET));
		//获取每页显示多少条数据
		int limit = request.getParameter(Const.BOOTSTRAP_LIMIT) == null ? 10 : Integer.valueOf(request.getParameter(Const.BOOTSTRAP_LIMIT));
		//获得bootstrap自带模糊搜索条件值
		String bootstrapSearch = request.getParameter(Const.BOOTSRRAP_SEARCH);
		//获取部门等级编码用于查询本级及下级部门数据(xiaogq)
		String deptScope = request.getParameter(Const.DEPT_SCOPE) == null ? "" : request.getParameter(Const.DEPT_SCOPE);
		entity.setSort(sort);
		entity.setOrder(order);
		entity.setOffset(offset);
		entity.setLimit(limit);
		entity.setSearch(bootstrapSearch);
		entity.setDeptScope(deptScope);
        //获取搜索条件Json字符串
		String mulitSearch = request.getParameter(Const.MUTILSEARCH);
		Set<Condition> mulitSearchSet = getMulitSearch(mulitSearch, STable.TABLE_HEAD+tableCode);
		entity.setMutilSearch(mulitSearchSet);
		return entity;
	}

    /**
	 * 封装搜索条件数据方法(xiaogqxm)
	 * @param mulitSearch
	 * @param tableName
	 * @return
     */
	public static Set<Condition> getMulitSearch(String mulitSearch,String tableName) {
		Set<Condition> mulitSearchList = new LinkedHashSet<Condition>();
		if ((mulitSearch != null) && (!mulitSearch.trim().equals(""))) {
			try {
				JSONArray josnArray = new JSONArray(mulitSearch);
				int length = josnArray.length();
				for (int i = 0; i < length; i++) {
					JSONObject jObject = new JSONObject(josnArray.get(i).toString());
					String filterName = jObject.getString(Const.MUTIL_SEARCH_NAME);
					String filterValue = StringUtils.replace(jObject.getString(Const.MUTIL_SEARCH_VALUE), "$^%",",").replace("'", "");
					if ((!filterName.equals("pk_sys_custom_entity_type_1"))|| (!filterValue.equals("0"))) {
						try {
							Condition condition = new Condition(new SField(filterName, new STable(tableName)), Condition.ConditionType.valueOf(jObject.getString(Const.MUTIL_SEARCH_CONDICTION)), filterValue);
							mulitSearchList.add(condition);
						} catch (RuntimeException e) {
							e.printStackTrace();
							//log.error("解析多条件检索数据错误：" + e);
						}
					}
				}
			} catch (JSONException e) {
				e.printStackTrace();
				//log.error("查询语句中多条件检索数据解析发生异常：" + e);
			}
		}
		return mulitSearchList;
	}
}
